﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using HigLabo.WindowsAzure.RestService.Extensions;

namespace HigLabo.WindowsAzure.RestService.ServiceManagement
{
    public class AffinityGroup
    {
        public static readonly String ElementName = "AffinityGroup";

        public String Name { get; private set; }
        public String Label { get; private set; }
        public String Description { get; private set; }
        public String Location { get; private set; }

        private Collection<HostedService> _HostedServices = new Collection<HostedService>();
        public IEnumerable<HostedService> HostedServices
        {
            get { foreach (var item in _HostedServices) { yield return item; } }
        }

        private Collection<StorageService> _StorageServices = new Collection<StorageService>();
        public IEnumerable<StorageService> StorageServices
        {
            get { foreach (var item in _StorageServices) { yield return item; } }
        }

        public AffinityGroup(XElement xml)
        {
            IEnumerable<XElement> els; XElement el;
            if (xml == null)
            {
                throw new ArgumentNullException("xml");
            }
            this.Name = xml.ValueOrEmpty("Name");
            this.Label = Base64Converter.From(xml.ValueOrEmpty("Label"));
            this.Description = xml.ValueOrEmpty("Description");
            this.Location = xml.ValueOrEmpty("Location");

            el = xml.ElementByLocalName("HostedServices");
            if (el != null)
            {
                els = el.ElementsByLocalName(HostedService.ElementName);
                foreach (var item in els)
                {
                    var add = new HostedService(item);
                    this._HostedServices.Add(add);
                }
            }

            el = xml.ElementByLocalName("StorageServices");
            if (el != null)
            {
                els = el.ElementsByLocalName(StorageService.ElementName);
                foreach (var item in els)
                {
                    var add = new StorageService(item);
                    this._StorageServices.Add(add);
                }
            }
        }
    }
}
